﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace TeBigCommonUI.Converter
{
    public class RectConverter : IMultiValueConverter
    {
        private bool TryParse(object o, out double v)
        {
            return double.TryParse(o.ToString(), out v);
        }

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType == typeof(Rect) && values.Length == 4)
            {
                if (TryParse(values[0], out double x) && TryParse(values[1], out double y) && TryParse(values[2], out double w) && TryParse(values[3], out double h))
                {
                    return new Rect(x, y, w, h);
                }
            }

            return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
